package by.pavlyukevich.paymentsystem.panel.user;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.ResourceModel;
import org.springframework.orm.jpa.JpaSystemException;

import by.pavlyukevich.paymentsystem.add.AbstractDataProvider;
import by.pavlyukevich.paymentsystem.add.ErrorEvent;
import by.pavlyukevich.paymentsystem.add.PanelUpdateEvent;
import by.pavlyukevich.paymentsystem.dataaccess.filter.Filter;
import by.pavlyukevich.paymentsystem.dataaccess.filter.IndividualFilter;
import by.pavlyukevich.paymentsystem.dataaccess.filter.UserAccountFilter;
import by.pavlyukevich.paymentsystem.datamodel.user.Individual;
import by.pavlyukevich.paymentsystem.datamodel.user.Individual_;
import by.pavlyukevich.paymentsystem.panel.MyPagingNavigator;
import by.pavlyukevich.paymentsystem.services.user.IndividualService;

public class IndividualListPanel extends Panel {

	@Inject
	private IndividualService individualService;
	private IndividualFilter individualFilter;
	private Panel respPanel;

	public IndividualListPanel(String id) {
		this(id, new IndividualFilter());
	}

	public IndividualListPanel(String id, IndividualFilter individualFilter) {
		super(id);
		this.individualFilter = individualFilter;
		this.respPanel = this;
	}

	@Override
	protected void onInitialize() {
		setOutputMarkupId(true);

		IndividualDataProvider individualDP = new IndividualDataProvider(Individual_.family,
				individualFilter);

		final WebMarkupContainer individualTable = new WebMarkupContainer("individual-table");
		individualTable.setOutputMarkupId(true);
		add(individualTable);

		DataView<Individual> dataView = new DataView<Individual>("individual-row", individualDP, 20) {

			@Override
			protected void populateItem(Item<Individual> item) {

				final Individual individual = item.getModelObject();

				item.add(new Label("family"));
				item.add(new Label("name"));
				item.add(DateLabel.forDatePattern("birthday", "dd.MM.yyyy"));
				item.add(new Label("email"));

				item.add(new AjaxLink<Void>("userAccounts") {
					@Override
					public void onClick(AjaxRequestTarget target) {
						send(getPage(), Broadcast.BREADTH,
								new PanelUpdateEvent(target, new UserAccountListPanel("ac-panel",
										new UserAccountFilter().setIndividual(individual))));
					}
				});

				item.add(new AjaxLink<Void>("delete") {
					@Override
					public void onClick(AjaxRequestTarget target) {
						try {
							individualService.delete(individual);
							target.add(respPanel);
						} catch (JpaSystemException e) {
							error(new ResourceModel("t.error.unableToDelete").getObject());
							send(getPage(), Broadcast.BREADTH, new ErrorEvent(target));

						}
					}
				});

				item.add(new AjaxLink<Void>("edit") {
					@Override
					public void onClick(AjaxRequestTarget target) {
						send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
								new IndividualEditPanel("ac-panel", individual, respPanel)));
					}
				});
			}

		};

		individualTable.add(dataView);
		add(new AjaxLink<Void>("new") {

			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new IndividualEditPanel("ac-panel", new Individual(), respPanel)));
			}
		});

		add(new MyPagingNavigator("paging", dataView));

		add(new OrderByBorder<SingularAttribute<Individual, ?>>("sortByFamily", Individual_.family,
				individualDP));
		add(new OrderByBorder<SingularAttribute<Individual, ?>>("sortByName", Individual_.name,
				individualDP));

		super.onInitialize();
	}

	private class IndividualDataProvider extends AbstractDataProvider<Individual> {
		public IndividualDataProvider(SingularAttribute<Individual, ?> sortParam,
				Filter<Individual> filter) {
			super(sortParam, filter, individualService);
		}
	};

}
